package com.his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.his.entity.CaseHistory;
import com.his.entity.Registration;
import com.his.entity.ResponseResult;
import com.his.mapper.CaseHistoryMapper;
import com.his.mapper.RegistrationMapper;
import com.his.service.ICaseHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 病历表 服务实现类
 * </p>
 *
 * @author Demoon Liu
 * @since 2022-02-20
 */
@Service
public class CaseHistoryServiceImpl extends ServiceImpl<CaseHistoryMapper, CaseHistory> implements ICaseHistoryService {

    @Resource
    public CaseHistoryMapper caseHistoryMapper;

    @Resource
    public RegistrationMapper registrationMapper;


    @Override
    public ResponseResult submit(CaseHistory caseHistory) {
        int code;
        String message;
        long id = caseHistory.getRegistrationid();
        Registration registration = new Registration();
        registration.setId(id);
        registration.setEndattendance(4);

        if(caseHistoryMapper.updateById(caseHistory)>0) {   //提交初审信息
            registrationMapper.updateById(registration);   //修改为确诊
            message = "修改成功";
            code = 201;
        }
        else {
            message = "修改失败";
            code = 400;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("caseHistory", caseHistory);

        return new ResponseResult(code, message,map);
    }

    @Override
    public ResponseResult selectIncomplete() {
        int code;
        String message;

        QueryWrapper<CaseHistory> wrapper = new QueryWrapper<>();
        // id 在子查询中查出来
        wrapper.inSql("registrationid","select id from registration where id=2");

     //   List<Object> objects = caseHistoryMapper.selectObjs(wrapper);
        List<Map<String, Object>> maps = caseHistoryMapper.selectMaps(wrapper);
        if(maps.size()>0) {
            message = "插入成功";
            code = 201;
        }
        else {
            message = "插入失败";
            code = 400;
        }
        maps.forEach(System.out::println);
        return new ResponseResult(code,message,maps);
    }

    @Override
    public ResponseResult selectComplete() {
        int code;
        String message;
        QueryWrapper<CaseHistory> wrapper = new QueryWrapper<>();
        // id 在子查询中查出来
        wrapper.inSql("registrationid","select id from registration where id=3");

        //   List<Object> objects = caseHistoryMapper.selectObjs(wrapper);
        List<Map<String, Object>> maps = caseHistoryMapper.selectMaps(wrapper);
        if(maps.size()>0) {
            message = "插入成功";
            code = 201;
        }
        else {
            message = "插入失败";
            code = 400;
        }
        maps.forEach(System.out::println);
        return new ResponseResult(code,message,maps);
    }
}
